var wxpay = require('../../utils/pay.js');
const utils = require('../../utils/util.js');
var app = getApp()
Page({
  data: {
    statusType: ["全部", "待付款", "待发货", "待收货", "待评价"],
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    loadingMoreHidden: false,
    currPage: 1,
    showQRcode: false,
    QrCodeinfo: {}
  },

  // 二维码展示
  showQRcode: function (e) {
    let that = this;
    let oid = e.currentTarget.dataset.oid;
    this.setData({
      showQRcode: true
    })
    var timer = setInterval(function () {
      wx.request({
        url: app.globalData.host + '/api/Order/GetOrderQrCode?oid=' + oid,
        success: function (res) {
          if (res.data.Code == 0) {
            if (res.data.Data.QrCode) {
              let QrCodeInfo = res.data.Data;
              QrCodeInfo.QrCode = utils.formatAvater(QrCodeInfo.QrCode, 'https://static.711688.net.cn/upload/ordercode/');
              if (QrCodeInfo.CheckUid || !that.data.showQRcode) {
                clearInterval(timer);
              }
              that.setData({
                QrCodeinfo: QrCodeInfo
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data, Message,
              showCancel: false
            })
          }
        }
      })
    }, 3000)
  },
  closeQRcode: function () {
    this.setData({
      showQRcode: false
    })
  },

  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType,
      currPage: 1
    });
    this.onShow();
  },

  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },

  pingjia: function (e) {
    var oid = e.target.dataset.oid;
    var osn = e.target.dataset.osn;
    wx.navigateTo({
      url: "/pages/pingjia/index?oid=" + oid + "&osn=" + osn
    })
  },

  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.host + '/api/order/CancelOrder',
            data: {
              uid: app.globalData.userInfo.Uid,
              oid: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.Code == 0) {
                that.onShow();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  //申请退款
  cancelOrderPay: function (e) {
    var that = this;
    var oid = e.currentTarget.dataset.oid;
    wx.showModal({
      title: '提示',
      content: '确认要申请退款吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Order/CancelPay?oid=' + oid,
            success: (res) => {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '申请成功',
                  icon: 'success',
                  duration: 2000
                })
                that.onShow();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  toPayTap: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wxpay.wxpay(app, orderId, "/pages/order-list/index");
  },
  onLoad: function (e) {
    // 生命周期函数--监听页面加载
    if (e.t) {
      this.setData({
        currentType: e.t
      })
    }
  },

  getOrderStatistics: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/order/statistics',
      data: { uid: app.globalData.userInfo.Uid },
      success: (res) => {
        if (res.data.Code == 0) {
          var tabClass = that.data.tabClass;
          if (res.data.Data.count_id_no_pay > 0) {
            tabClass[1] = "red-dot"
          }
          if (res.data.Data.count_id_no_transfer > 0) {
            tabClass[2] = "red-dot"
          }
          if (res.data.Data.count_id_no_confirm > 0) {
            tabClass[3] = "red-dot"
          }
          if (res.data.Data.count_id_success > 0) {
            tabClass[4] = "red-dot"
          }

          that.setData({
            tabClass: tabClass,
          });
        }
      }
    })
  },

  onShow: function () {
    // 获取订单列表
    wx.showLoading();
    var that = this;
    that.getOrderStatistics();
    var postData = {
      uid: app.globalData.userInfo.Uid,
      orderState: 0,
      page: that.data.currPage
    };
    if (that.data.currentType == 1) {
      postData.orderState = 20
    }
    if (that.data.currentType == 2) {
      postData.orderState = 80
    }
    if (that.data.currentType == 3) {
      postData.orderState = 100
    }
    if (that.data.currentType == 4) {
      postData.orderState = 120
    }
    this.getOrderStatistics();
    wx.request({
      url: app.globalData.host + '/api/order/GetUserOrders',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.Code == 0) {
          var orderList = res.data.Data.OrderList;
          for (var i = 0; i < orderList.length; i++) {
            for (var j = 0; j < orderList[i].ProductInfos.length; j++) {
              if (orderList[i].ProductInfos[j]) {
                orderList[i].ProductInfos[j].ShowImg = app.globalData.image190_190host + orderList[i].ProductInfos[j].ShowImg;
                orderList[i].IsReview = orderList[i].ProductInfos[j].IsReview
              }

            }
          }
          if (that.data.currPage != 1) {
            var prenews = that.data.orderList
            orderList = prenews.concat(orderList);
          }
          that.setData({
            orderList: orderList,
            loadingMoreHidden: res.data.Data.PageModel.HasNextPage
          });
        } else {
          this.setData({
            orderList: null
          });
        }
      }
    })
  },

  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.currPage;
    that.setData({
      currPage: currPage + 1
    })
    that.onShow();
  },

  confirmBtnTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认您已收到商品？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: app.globalData.host + '/api/order/ReceiveOrder',
            data: {
              uid: app.globalData.userInfo.Uid,
              oid: orderId
            },
            success: (res) => {
              wx.hideLoading();
              if (res.data.Code == 0) {
                that.onShow();
              }
            }
          })
        }
      }
    })
  },

  onUnload: function () {
    // 生命周期函数--监听页面卸载
    wx.redirectTo({
      url: "/pages/member/index"
    });
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    wx.stopPullDownRefresh();
  }
})